<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>default-event-rule-addExprMsg</title>
  	<%@ include file="../include/common.jsp"%>
    <%@ include file="../include/jbox.jsp"%>
    
    <script type="text/javascript">
    	var propId = -1;
	    $(function(){
			initData();
			if('${flag}' == 'expr'){
				$("#viewMsgId").css({"display":"none"});
			}else{
				$("#viewMsgId").css({"display":""});
			}
		})
		function closewin(){
			  getTopWin(window).closejBox("default-event-rule-addExprMsg");
		}
		function addRuleProp(){
			insertAtCaret(document.getElementById("ruleText"),$("#propRuleSelect").val());
			$.ajax({
				   type: "POST", 
				   url: "${pageContext.request.contextPath}/event/event-rule!havePropIdbyPropName.action?propRuleSelect="+encodeURI(encodeURI($("#propRuleSelect").val())), 
				   data: '', //传的参数
				   dataType: 'json',
				   success: function(data){ //结果
					   propId = data+'L';
				   }
			});
		}
		function addRuleAttr(){
			insertAtCaret(document.getElementById("ruleText"),$("#attrRuleSelect").val());
		}
		function addRuleFun(){
			if('resname' == $("#funRuleSelect").val()){
				insertAtCaret(document.getElementById("ruleText"),"resname($resid)");
			}
			else if('ip' == $("#funRuleSelect").val()){
				insertAtCaret(document.getElementById("ruleText"),"ip($resid)");
			}
			else if('inc' == $("#funRuleSelect").val()){
				insertAtCaret(document.getElementById("ruleText"),"inc($resid,$propId,$time)");
			}
			else{
				insertAtCaret(document.getElementById("ruleText"),$("#funRuleSelect").val());
			}
		}
		function initData(){
			$.ajax({
				   type: "post", 
				   url: "${pageContext.request.contextPath}/event/default-event-rule!propertyList.action?resTypeId="+${resTypeId},
				   data: "", //传的参数
				   async:false,
				   dataType: "json",
				   success: function(data){ //结果
					   var obj= document.getElementById("propRuleSelect");
					   obj.length=0;
					   if(data != null){
						   for(var i = 0 ;i<data.length;i++){
								obj.options[i]=new Option(data[i].name,data[i].id);
					   	   }
					   }
				   }
				  
			});
			 $.ajax({
				   type: "post", 
				   url: "${pageContext.request.contextPath}/event/default-event-rule!attributeList.action?resTypeId="+${resTypeId},
				   data: "", //传的参数
				   async:false,
				   dataType: "json",
				   success: function(data){ //结果
					   var obj= document.getElementById("attrRuleSelect");
					   obj.length=0;
					   if(data != null){
						   for(var i = 0 ;i<data.length;i++){
								obj.options[i]=new Option(data[i].name,data[i].id);
					   	   }
					   }
				   }
				  
			});
		}
		function storeRuleCaret (textEl){
		      if (textEl.createTextRange){
			      textEl.caretPos = document.selection.createRange().duplicate();
		      }
		 }
		 function insertAtCaret (textEl, text) {
		      if (textEl.createTextRange && textEl.caretPos) {
		        var caretPos = textEl.caretPos;
		        caretPos.text =caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?text + ' ' : text;
		      }else{
		    	  textEl.value = text;
		      }
		 }
		 function add() {
			 $.ajax({
				   type: "post", 
				   url: "${pageContext.request.contextPath}/event/default-event-rule!validateExpress.action?resTypeId="+${resTypeId}, //请求的action
				   data: "express="+encodeURI(encodeURI($("#ruleText").val())),
				   dataType: "json",
				   success: function(data){ //结果
					   	  if(data == true){
					   		var pWin = getTopWin(window).currentWin["default-event-rule-addExprMsg"];
							pWin.addExprOrMsgValue($("#ruleText").val().replace( /(^\s*)|(\s*$)/g,''),'${flag}');
							getTopWin(window).closejBox("default-event-rule-addExprMsg");
					   	  }else{
					   		getTopWin(window).getMessageBox().alert("表达式格式错误","提示");
					   	  }
				   }
			});
		 }
		 function view(){
			var url ="iframe:event/default-event-rule!viewMsg.action?msg="+encodeURI(encodeURI($("#ruleText").val().replace( /(^\s*)|(\s*$)/g,'')))+"&resTypeId="+'${resTypeId}';
			var opt = { title: "消息预览",width:400,height:100,buttons: {}};
			getTopWin(window).openjBox(url,opt,'default-event-rule-viewmsg',window);
		 }
	</script>
  </head>
  
  <body>
    <form>
    	<table class="table_eidt">
    		<tr>
			    <td>
			    	<select name="propRuleSelect" id="propRuleSelect" onchange="addRuleProp();" style="width: 185px;">
					</select>
					<select name="attrRuleSelect" id="attrRuleSelect" onchange="addRuleAttr();" style="width: 185px;">
					</select>
					<select name="funRuleSelect" id="funRuleSelect" onchange="addRuleFun();" style="width: 185px;">
						 <option value="">选择函数</option>
						 <option value="resname">资源名称</option>
						 <option value="ip">资源IP</option>
						 <option value="round(d)">四舍五入</option>
						 <option value="round(d,2)">四舍五入(值,2)</option>
						 <option value="inc">增长率</option>
					</select>
			    </td>
			  </tr>
			  <tr>
			    <td colspan="3">
					<textarea name="ruleText" style="width: 565px;height: 260px" id="ruleText" onselect="storeRuleCaret(this);" onclick="storeRuleCaret(this);" onkeyup="storeRuleCaret(this);">${exprOrMsg}</textarea>
				</td>
			  </tr>		
    	</table>
    	<div align="center" style="margin-top:5px;">
    		<input type="button" id="viewMsgId" value="消息预览" onclick="view();" class="btn-4" style="display: none;">
		    <input type="button" value="提交" class="btn-4" onclick="add();">
		    <input type="button" value="返回" onclick="closewin();" class="btn-4">
    	</div>
    </form>
  </body>
</html>
